/*
 * @lc app=leetcode.cn id=101 lang=cpp
 *
 * [101] 对称二叉树
 *
 * https://leetcode-cn.com/problems/symmetric-tree/description/
 *
 * algorithms
 * Easy (57.43%)
 * Likes:    1851
 * Dislikes: 0
 * Total Accepted:    553.8K
 * Total Submissions: 964.2K
 * Testcase Example:  '[1,2,2,3,4,4,3]'
 *
 * 给你一个二叉树的根节点 root ， 检查它是否轴对称。
 * 
 * 
 * 
 * 示例 1：
 * 
 * 
 * 输入：root = [1,2,2,3,4,4,3]
 * 输出：true
 * 
 * 
 * 示例 2：
 * 
 * 
 * 输入：root = [1,2,2,null,3,null,3]
 * 输出：false
 * 
 * 
 * 
 * 
 * 提示：
 * 
 * 
 * 树中节点数目在范围 [1, 1000] 内
 * -100 <= Node.val <= 100
 * 
 * 
 * 
 * 
 * 进阶：你可以运用递归和迭代两种方法解决这个问题吗？
 * 
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    // //递归法
    // bool compare(TreeNode* left, TreeNode* right){
    //     //确定终止条件
        
    //     //首先确定是不是走到头了，就是left==NULL && right==right
    //     if (left == NULL && right == NULL) return true;

    //     //在判断 一边有值一边为空的情况，也走到头了，但是false
    //     else if ((left == NULL && right != NULL) || (left != NULL && right == NULL)) return false;

    //     //接下来判断 两边都不为NULL
    //     //而且当两边的值 不相等的时候，也是走到头了，但是false
    //     else if (left->val != right->val) return false;

    //     //剩下的情况 就是 left->val == right->val
    //     bool outside = compare(left->left, right->right);
    //     bool inside = compare(left->right, right->left);

    //     return outside && inside;
    // }
    // bool isSymmetric(TreeNode* root) {
    //     if(root == NULL) return true;
    //     return compare(root->left, root->right);
    // }


    //迭代法--用队列queue
    bool isSymmetric(TreeNode* root) {

        if(root == NULL) return true;

        queue<TreeNode*> que;
        TreeNode* curLeft = root->left;
        TreeNode* curRight = root->right;

        que.push(curLeft);
        que.push(curRight);

        while(!que.empty()){
            curLeft = que.front();
            que.pop();       
            curRight = que.front();
            que.pop();

            if(curRight == NULL && curLeft == NULL) continue;

            if((curLeft != NULL && curRight == NULL) 
             || (curLeft == NULL && curRight != NULL)
             || (curLeft->val != curRight->val)) return false;

             que.push(curLeft->left);
             que.push(curRight->right);
             que.push(curLeft->right);
             que.push(curRight->left);
        }

        return true;

    }


    // //迭代法--用堆栈stack
    //     bool isSymmetric(TreeNode* root) {

    //     if(root == NULL) return true;

    //     stack<TreeNode*> st;
    //     TreeNode* curLeft = root->left;
    //     TreeNode* curRight = root->right;

    //     st.push(curLeft);
    //     st.push(curRight);

    //     while(!st.empty()){
    //         curRight = st.top();
    //         st.pop();
    //         curLeft = st.top();
    //         st.pop();

    //         if(curRight == NULL && curLeft == NULL) continue;

    //         if((curLeft != NULL && curRight == NULL) 
    //          || (curLeft == NULL && curRight != NULL)
    //          || (curLeft->val != curRight->val)) return false;

    //          st.push(curLeft->left);
    //          st.push(curRight->right);
    //          st.push(curLeft->right);
    //          st.push(curRight->left);
    //     }

    //     return true;

    // }

    
};
// @lc code=end

